<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>

<style>
    .parsley-errors-list{
        display: none;
    }
</style>
<script>
    /*选择价表自动补全*/
    function autoCompleteDrugs(inputId,idex){
        var storageId = $("#applyTableId .table-selected").find("td").eq(0).find("input").val();
        var url=ctx+"/doctor/prescriptionItem/autoComplete?drugClass=B&storageId="+storageId;
        autoComplete(inputId,url,
                function(data){
                    var rows = [];
                    for(var i=0; i<data.length; i++){
                        rows[rows.length] = {
                            data:data[i],
                            value:data[i].priceId,
                            result:data[i].drugName
                        };
                    }
                    return rows;
                },function(data, i, max){
                    return data.inputCode+"|"+data.drugName+"|"+data.packageUnit+"|"+data.price+"元 |"+data.firmName+"|库存量："+data.quantity;
                },function(event,data,formatted){
                    /*判断选中药品是否已添加，如果添加，则不让再次添加*/
                    var bl = true;
                    $("#herbal_ul li div span input").each(function(){
                        if(($(this).val()==data.drugCode)&&($(this).attr('id')!='drugCode'+idex)){
                            bl = false;
                            return false;
                        }
                    });
                    if(bl){
                        var code=$('#drugCode'+idex).val();
                        if(code==null || code ==""){
                            addDrugList('herbalLength');
                        }
                        $('#drugCode'+idex).attr('value',data.drugCode);
                        $('#singleDosage'+idex).attr('value',data.dosePerUnit);
                        $('#singleUnit'+idex).attr('value',data.minUnits);
                        $('#firmId'+idex).attr('value',data.firmId);
                        $('#firmName'+idex).attr('value',data.firmName);
                        $('#price'+idex).attr('value',data.price);
                        $('#packageSpec'+idex).attr('value',data.packageSpec);
                        $('#sumPrice'+idex).attr('value',data.price);
                        $('#packageUnit'+idex).attr('value',data.packUnit);
                        $('#spec'+idex).attr('value',data.minSpec);
                        $('#unit'+idex).attr('value',data.minUnits);
                        $('#inputCode'+idex).attr('value',data.inputCode);
                        $('#priceId'+idex).attr('value',data.priceId);
                        $('#gram'+idex).html(data.packageUnit);
                        $("#quantity").attr('value',data.quantity);

                    }else{
                        $('#drugNameIds'+idex).val('');
                        toastr.info("该药品已添加，不能重复添加！");
                        return false;
                    }

                });
    }

    function addDrugList(id,type){
        var charge = $("#applyTableId .table-selected").find("td").eq(4).find("input").val();
        var status = $("#applyTableId .table-selected").find("td").eq(5).find("input").val();
        if ((charge==0 || charge=='0') && (status==0 ||status=='0')){
            rowNum =$("#"+id).val();
            var html= "";
            html+='<li id="c_li_'+rowNum+'">' +
            '<div>' +
            '<span class="col-sm-6">' +
            '<input name="itemList['+rowNum+'].drugName" class="form-control" data-parsley-required="true" placeholder="选择药品" id="drugNameIds'+rowNum+'" value=""/> '+
            '</span>'+
            '<span class="col-sm-4">' +
            '<input type="hidden" name="itemList['+rowNum+'].spec" id="spec'+rowNum+'" value=""/>' +
            '<input type="hidden" name="itemList['+rowNum+'].inputCode" id="inputCode'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].drugCode" id="drugCode'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].firmId" id="firmId'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].firmName" id="firmName'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].singleDosage" id="singleDosage'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].singleUnit" id="singleUnit'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].price" id="price'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].packageUnit" id="packageUnit'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].packageSpec" id="packageSpec'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].abidance" id="abidance'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].sumPrice" id="sumPrice'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].unit" id="unit'+rowNum+'" value=""/>'+
            '<input type="hidden" name="itemList['+rowNum+'].priceId" id="priceId'+rowNum+'" value=""/>'+
            '<input type="hidden" id="quantity" value=""/>'+
            '<input type="text" name="itemList['+rowNum+'].num" id="num'+rowNum+'" data-parsley-required="true" class="form-control" onkeyup="onKeyPress(\'num'+rowNum+'\')"  placeholder="数量" value="1" />'+
            '</span>'+
            '<span class="col-sm-1 gram" id="gram'+rowNum+'"></span> <i class="col-sm-1 fa fa-close" style="float:right;" onclick="delDrug('+rowNum+')"></i>'+
            '<span class="col-sm-12 mt10"><input placeholder="备注" name="itemList['+rowNum+'].remarks" class="col-sm-12" style="height: 30px;" type="text"></span>'+
            '</div>'+
            '</li>';
            $("#herbal_ul").append(html);
            autoCompleteDrugs('drugNameIds'+rowNum+'',rowNum);
            rowNum++;
            $("#"+id).val(rowNum);
            html="";
        }else{
            toastr.info("药品已保存或已生成药品费用，不能进行药品添加！");
        }

    }
    function delDrug(idx){
        /* if ($("#applyTableId .table-selected").length>0){
         var charge = $("#applyTableId .table-selected").find("td").eq(4).find("input").val();
         if (charge==0||charge=='0'){*/
        $("#c_li_"+idx).remove();
        /* }else{
         toastr.info("药品已收费或已生成药品费用，不能进行药品添加！");
         return false;
         }

         }*/
    }

    //验证开方用量不能大于库存量
    function onKeyPress(inputQuantityNum){
        var quantityNum = $("#"+inputQuantityNum).val();/*输入量*/
        var quantity = $("#quantity").val();/*库存量*/
        if(quantityNum && quantity){
            if(parseInt(quantityNum)>parseInt(quantity)){
                toastr.info("开方用量不能大于库存量");
                $("#inputQuantityNum").val(quantity);
            }
        }

    }

</script>
<div class="panel-body">
    <div class="btn-group">
        <button type="button" class="btn btn-success" onclick="addDrugList('herbalLength','1')">添加药品</button>
    </div>
    <form id="prescChineseItemForm" data-parsley-validate="" onsubmit="return formSaveLoad('prescChineseItemDiv','inputItemForm','${ctx}/doctor/prescriptionItem/save','${ctx}/doctor/prescriptionItem/chineseIndex?applyId=${prescriptionItem.applyId}');" method="post" class="form-horizontal">
        <div class="col-lg-10 pull-right">
            <label class="col-sm-2 control-label">是否代煎：</label>
            <div class="col-sm-2">
                <select name="isDecoction" id="isDecoction" class="form-control" data-parsley-required="true">
                    <c:forEach items="${clinicDict}" var="dict">
                        <option value="${dict.id}">${dict.label}</option>
                    </c:forEach>
                    <option value="1">不代煎</option>
                </select>
            </div>
            <label class="col-sm-1 control-label">途径：</label>
            <div class="col-sm-2">
                <select name="channel" id="channel" class="form-control" data-parsley-required="true">
                    <c:forEach items="${administrationDicts}" var="dict">
                        <option value="${dict.id}">${dict.label}</option>
                    </c:forEach>
                </select>
            </div>

            <label class="col-sm-1 control-label">频次：</label>
            <div class="col-sm-2">
                <select name="frequency" id="frequency" class="form-control" data-parsley-required="true">
                    <c:forEach items="${frequency}" var="dict">
                        <option value="${dict.id}">${dict.freqDesc}</option>
                    </c:forEach>
                </select>
            </div>
            <label class="col-sm-1 control-label">剂数：</label>
            <div class="col-sm-1"><input name="repetition" value="7" class="form-control" ></div>
        </div>
        <ul id="herbal_ul" class="drugCenterUl">
            <input type="hidden" name="applyId" value="${prescriptionItem.applyId}">
            <input type="hidden" id="herbalLength" value="${chinaList.size()}">
            <c:if test="${chinaList.size()>0}">
                <c:forEach items="${chinaList}" var="item" varStatus="idx">
                    <li>
                        <div>
                            <span class="col-sm-8">
                                   <input name="itemList[${idx.index}].drugName" class="form-control"  placeholder="选择药品" id="drugNameIds${idx.index}" value="${item.drugName}" />
                           </span>
                            <span class="col-sm-3">
                                <input type="hidden" name="itemList[${idx.index}].spec" id="spec${idx.index}" value="${item.spec}"/>
                                <input type="hidden" name="itemList[${idx.index}].inputCode" id="inputCode${idx.index}" value="${item.inputCode}"/>
                                <input type="hidden" name="itemList[${idx.index}].drugCode" id="drugCode${idx.index}" value="${item.drugCode}"/>
                                 <input type="hidden" name="itemList[${idx.index}].firmId" id="firmId${idx.index}" value="${item.firmId}"/>
                                <input type="hidden" name="itemList[${idx.index}].firmName" id="firmName${idx.index}" value="${item.firmName}"/>
                                <input type="hidden" name="itemList[${idx.index}].singleDosage" id="singleDosage${idx.index}" value="${item.singleDosage}"/>
                                <input type="hidden" name="itemList[${idx.index}].singleUnit" id="singleUnit${idx.index}" value="${item.singleUnit}"/>
                                <input type="hidden" name="itemList[${idx.index}].price" id="price${idx.index}" value="${item.price}"/>

                                <input type="hidden" name="itemList[${idx.index}].packageUnit" id="packageUnit${idx.index}" value="${item.packageUnit}"/>
                                <input type="hidden" name="itemList[${idx.index}].packageSpec" id="packageSpec${idx.index}" value="${item.packageSpec}"/>
                                <input type="hidden" name="itemList[${idx.index}].abidance" id="abidance${idx.index}" value="${item.abidance}"/>
                                <input type="hidden" name="itemList[${idx.index}].sumPrice" id="sumPrice${idx.index}" value="${item.sumPrice}"/>
                               <input type="hidden" name="itemList[${idx.index}].unit" id="unit${idx.index}" value="${item.unit}"/>
                                <input type="text" name="itemList[${idx.index}].num" id="num${idx.index}" class="form-control"  placeholder="数量" value="${item.num}"/>
                            </span>
                            <span class="col-sm-1 gram">${fns:getDictLabel(item.packageUnit,'SPEC_UNIT' ,'')}</span>
                            <span class="col-sm-12 mt10"><input placeholder="备注" name="itemList[${idx.index}].remarks" class="col-sm-12 form-control"  value="${item.remarks}"  style="height: 30px;" type="text"></span>

                        </div>

                    </li>
                </c:forEach>
            </c:if>
        </ul>
    </form>

</div>




